#!/usr/bin/env python
"""
Overwrite copyright.py when doing a release
"""
import sys
from string import Template
import datetime
import os.path
from mercurial import ui, hg, commands

dn = os.path.dirname

def run(argv=None):
    if argv is None:
        argv = sys.argv[:]

    ver = argv[1]

    releaseDate = datetime.date.today().isoformat()

    d = os.path.abspath(dn(dn(__file__)))
    hgui = ui.ui(quiet=True)
    hgui.pushbuffer()
    hgrepo = hg.repository(ui=hgui, path=d)
    commands.identify(hgui, hgrepo)
    rev = hgui.popbuffer().strip()

    module = Template('''"""
Indomnify is copyright (c) 2011 Decipher Inc.

GENERATED FILE -- DO NOT EDIT
"""
from datetime import date

author = "Decipher Inc."
releaseDate = "$releaseDate"
releaseRevision = "$rev"
__version__ = "$ver"
''')
    f = open(d + '/indomnify/copyright.py', 'w')
    f.write(
            module.substitute(releaseDate=releaseDate,
                rev=rev,
                ver=ver,
                ))
    return 0

if __name__ == '__main__':
    sys.exit(run())
